package com.rd.student.ruanjian.class3.gaochenxi.day0724;

import java.util.concurrent.locks.ReentrantLock;

//使用lock
public class second {

      static ReentrantLock lock=new ReentrantLock();
      private static int num=0;

      public static void main(String[] args) {
            new A().start();
            new B().start();
            new C().start();

      }
      static class A extends Thread{

            public void run(){
                  for(int i=0;i<10;){
                        try {
                              lock.lock();
                              while(num%3==0){
                                    System.out.print("A");
                                    num++;
                                    i++;
                              }
                        } finally {
                              lock.unlock();
                        }
                  }
            }
      }

      static class B extends Thread{

            public void run(){
                  for(int i=0;i<10;){
                        try {
                              lock.lock();
                              while(num%3==1){
                                    System.out.print("B");
                                    num++;
                                    i++;
                              }
                        } finally {
                              lock.unlock();
                        }
                  }
            }
      }

      static class C extends Thread{

            public void run(){
                  for(int i=0;i<10;){
                        try {
                              lock.lock();
                              while(num%3==2){
                                    System.out.print("C");
                                    num++;
                                    i++;
                              }
                        } finally {
                              lock.unlock();
                        }
                  }
            }
      }
}

